package com.example.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: ThymeleafController
 * @Author wenlinyu
 * @Date: 2020/12/15 14:26
 * @Version 1.0
 */
@Controller
public class ThymeleafController {

    @GetMapping("/")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView();
        // 设置跳转的视图 默认映射到 src/main/resources/templates/{viewName}.html
        view.setViewName("index");
        // 设置属性
        view.addObject("title","thymeleaf第一个Web页面");
        view.addObject("desc","第一个thymeleaf系统");
        Author author = new Author();
        author.setName("张三");
        author.setAge(24);
        author.setEmail("zhangsan@163.com");
        view.addObject("author",author);
        List<Author> list = new ArrayList<>();
        list.add(author);
        author = new Author();
        author.setName("李斯");
        author.setAge(20);
        author.setEmail("lisi@qq.com");
        list.add(author);
        author = new Author();
        author.setName("路人甲");
        author.setAge(25);
        author.setEmail("lurenjia@126.com");
        list.add(author);
        view.addObject("list",list);
        return view;
    }

    class Author {
        private String name;
        private int age;
        private String email;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }
    }
}
